package edu.uba.fcen.estimacion.estimacion.graphics;

import java.io.File;

import org.apache.commons.math.stat.correlation.PearsonsCorrelation;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.apache.log4j.Logger;

import edu.uba.fcen.estimacion.estimacion.Estimacion;
import edu.uba.fcen.estimacion.estimacion.EstimacionData;

public class GSvsCantPalabrasCorrelacion {

	private static final Logger logger = Logger.getLogger(GSvsCantPalabrasCorrelacion.class);
	private static final double[] agradoGS = {1.8, 2, 2.6, 2.8, 2.8, 1.8, 2.4, 1.8, 2.6, 2.4, 3, 2, 1.2, 1.8, 2.8, 2.2, 2.2, 2.4, 2, 2.2, 1.4, 1.6, 1, 1.2, 2.2, 2.4, 1.4, 2.4, 2.4, 1.8, 2.2, 2.2, 1.8, 2.4, 2.8, 2.4, 2.8, 2.4, 1.2, 2.2};
	private static final double[] activacionGS = {2.2, 1.4, 3, 2.2, 2.6, 2, 2.6, 1.8, 2.6, 3, 2.8, 1.6, 2, 2.6, 2, 2.4, 2.6, 1.6, 1.2, 1.6, 2.6, 1.2, 2, 2.4, 2.8, 2, 2.2, 2, 2.4, 2.4, 2.4, 2.2, 1.8, 1.6, 2.6, 2.2, 2.4, 2.4, 2.4, 2.2};
	private static final double[] imginabilidadGS = {1.4, 2, 3, 2.2, 2.4, 2.6, 2.6, 1.4, 2.4, 3, 1.6, 1.8, 2.6, 2.2, 2.4, 2, 2.6, 2.6, 1.8, 1, 2.6, 2.4, 3, 2.4, 2.4, 1.2, 2.6, 1.6, 2.8, 2.2, 3, 2.2, 2, 1.6, 2.4, 2.6, 1.8, 3, 2.2, 2.4};
	/**
	 * Calcula los valores de la correlacion a medida que se va incrementando la 
	 * cantidad de palabras que hay disponibles. Esto se hace contra los resultados del 
	 * Gold Standard
	 *  
	 * @param args 
	 * 			<ul>
	 * 			   <li> 1ro - el path al csv donde estan los valores de las medias </ul>
	 * 			   <li> 2do - el path al directorio del GS, con los archivos procesados por Freeling </ul>
	 * 			   <li> 3ro - la cantidad de palabras completas que hay en la base en el momento de la ejecución </ul>
	 * 			</ul>
	 */
	public static void main(String[] args) {
		String pathToCSV = args[0];
		String pathToGoldStandardDirectory = args[1];
		int limitDatabase = Integer.parseInt(args[2]);
		
		double[] values;
		File directoryBase = new File(pathToGoldStandardDirectory);
		StringBuilder agrBuilder = new StringBuilder();
		StringBuilder actBuilder = new StringBuilder();
		StringBuilder imgBuilder = new StringBuilder();
		StringBuilder coverageBuilder = new StringBuilder();
		
		agrBuilder.append("agrado = c(");
		actBuilder.append("act = c(");
		imgBuilder.append("img = c(");
		coverageBuilder.append("coverage = c(");
		
		for (int i = 250; i <= limitDatabase; i+=100) {
			Estimacion estimacion = new Estimacion(pathToCSV, i);
			values = IterateOverFileSet(estimacion, directoryBase);
			agrBuilder.append(values[0] + ",");
			actBuilder.append(values[1] + ",");
			imgBuilder.append(values[2] + ",");
			coverageBuilder.append(values[3] + ",");
			estimacion.closeDB();
		}
		System.out.println(agrBuilder.toString());
		System.out.println(actBuilder.toString());
		System.out.println(imgBuilder.toString());
		System.out.println(coverageBuilder.toString());

	}
	
	private static double[] IterateOverFileSet(Estimacion estimacion, File directoryBase) {
		EstimacionData data;
//		DescriptiveStatistics agrado = new DescriptiveStatistics();
//		DescriptiveStatistics img = new DescriptiveStatistics();
//		DescriptiveStatistics act = new DescriptiveStatistics();
		double[] agrado = new double[40];
		double[] img = new double[40];
		double[] act = new double[40];
		DescriptiveStatistics coverage = new DescriptiveStatistics();
		String[] fileNames = {"1.txt", "2.txt", "3.txt", "4.txt", "5.txt", "6.txt", "7.txt", "8.txt", "9.txt", "10.txt", 
						"11.txt", "12.txt", "13.txt", "14.txt", "15.txt", "16.txt", "17.txt", 
						"18.txt", "19.txt", "20.txt", "21.txt", "22.txt", "23.txt", "24.txt", 
						"25.txt", "26.txt", "27.txt", "28.txt", "29.txt", "30.txt", "31.txt", 
						"32.txt", "33.txt", "34.txt", "35.txt", "36.txt", "37.txt", "38.txt",
						"39.txt", "40.txt"};
		int index = 0;
		for(String fileName : fileNames) {
			File in = new File(directoryBase.getAbsoluteFile() +"/"+ fileName);
			if (in.getName().endsWith(".txt")) {
				data = estimacion.runOver(in.getAbsolutePath());
				Double currentAgrado = Double.valueOf(data.getMeans().getMeanAgrado());
				Double currentAct = Double.valueOf(data.getMeans().getMeanActivo());
				Double currentImg = Double.valueOf(data.getMeans().getMeanImaginabilidad());
				if (!currentAgrado.isNaN() && !currentAct.isNaN() && !currentImg.isNaN()) {
					agrado[index]=currentAgrado;
					act[index] = currentAct;
					img[index] = currentImg;
					coverage.addValue(data.getCoverage()*100);
				}else {
					System.out.println("There is a nan");
					System.out.println(in.getName());
					agrado[index] =0d;
					act[index] = 0d;
					img[index] = 0d;
					coverage.addValue(data.getCoverage()*100);
				}
				index++;
			}
		}
//		final double[] sortedValues = agrado.getSortedValues();
//		System.out.print("c(");
//		for (int i = 0; i < sortedValues.length; i++) {
//			System.out.print(sortedValues[i] + ",");
//		}
//		System.out.println("");
		double corAgrado = new PearsonsCorrelation().correlation(agrado, agradoGS);
		double corActivacion = new PearsonsCorrelation().correlation(act, activacionGS);
		double corImg = new PearsonsCorrelation().correlation(img, imginabilidadGS);
		return new double[] {corAgrado, corActivacion, corImg, coverage.getMean()};
	}

}
